//
//  AccountTool.m
//  CC
//
//  Created by Calvin on 5/13/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "AccountTool.h"
#import "AccountModel.h"

#define AccountFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"account.data"]

@implementation AccountTool
+(void)save:(AccountModel *)model
{
    [NSKeyedArchiver archiveRootObject:model toFile:AccountFilePath];
}

+(AccountModel *)accountModel
{
    AccountModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:AccountFilePath];
    NSDate *now =[NSDate date];
    if ([now compare:model.expires_time]==NSOrderedDescending) {
        model=nil;
    }
    return  model;
}

+(void)accessTokenWithParam:(AccessTokenParam *)param success:(void (^)(AccountModel *))success failure:(void (^)(NSError *))failure
{
    [self postWithUrl:@"https://api.weibo.com/oauth2/access_token" param:param resultClass:[AccountModel class] success:success failure:failure];
}
@end
